/* Created by Aquariuslt on 5/20/17.*/


navbarController.$inject = ['navMenuService', '$location', '$scope', '$rootScope'];
var $=require('jquery')
function navbarController(navMenuService, $location, $scope, $rootScope) {
    var vm = this;

    vm.menus = navMenuService.getMenus();
    try{
        var merchant = JSON.parse(sessionStorage.m);
        vm.merchant = merchant;
    }catch(err){
        console.log('redirect to login')
        $location.path('/login');
    }



    //broadcast receive to update storename
    $scope.$on('update', function (ev, v) {

        vm.merchant = v;
    })


    vm.notify = function () {

        var anchor=$('#notify-area').attr('href');
        if(anchor.indexOf('#')>=0){//order flag
            var s='order';
        }else{
            var s='verify';
        }
        console.log(s)
        //broadcast to notify order or verify
        $rootScope.$broadcast(s,'');
    }

    vm.logout = function () {
        sessionStorage.clear();
        navMenuService.logout();
        $location.path("/login");
    }

    var socket_merchant = io.connect('http://10.222.29.189:8888');

    var m = JSON.parse(sessionStorage.m)
    var merchant_id = m._id;
    socket_merchant.on('refer_front_end', function (msg) {

        var url = window.location.href;
        var state = url.split("#")[1];

        if (merchant_id == msg.merchant_id) {
            if (msg.data == 1) {

                if(state == "!/order"){
                    $("#notify-area").html("You have the new orders,pls ckeck out!")
                        .attr("href", "javascript:location.reload();");
                }else{
                    $("#notify-area").html("You have the new orders,pls ckeck out!")
                        .attr("href", "/#!/order");
                }

                $("#notify").show();

            } else if (msg.data == 2) {

                if(state == "!/order"){
                    $("#notify-area").html("You have the new assess,pls ckeck out!")
                        .attr("href", "javascript:location.reload();");
                }else{
                    $("#notify-area").html("You have the new assess,pls ckeck out!")
                        .attr("href", "/#!/order");
                }

                $("#notify").show();

            }
        }
    });

    socket_merchant.on("notify_state_change", function (mid, data) {
        console.log(mid);
        console.log(data);
        try{
            var m=data;
            m._id=mid;
            sessionStorage.m= JSON.stringify(m);
        }catch (err){
            console.log('err')
        }

        if (merchant_id == mid) {
            if (data.block == 0) {//
                if (data.state == 1) {// 通过
                    $("#notify-area").html("Verify success").attr("href", "/#!/food");
                    $("#notify").show();
                } else {
                    $("#notify-area").html("Refuse Reason: " + data.msg).attr("href", "javascript:location.reload();");
                    $("#notify").show();
                }
            } else {// blocked
                $("#notify-area").html("You have been Blocked").attr("href", "/#!/contact");
                $("#notify").show();
            }
        }


    });


    // unhide this view whenever visited

}

module.exports = navbarController;